﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SIMS.Global;
using SIMS.Database;
using System.Collections;
using System.Data;

namespace SIMS.School
{
    public class Period
    {
        public int Id { get; set; }
        public int CompanyId { get; set; }
        public string CompanyName { get; set; }

        public int CourseId { get; set; }
        public string CourseName { get; set; }

        public int CourseDetailId { get; set; }
        public string CourseDetailName { get; set; }

        public string Name { get; set; }
        public string Description { get; set; }

        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }

        public bool IsActive { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime ModifiedDate { get; set; }

        public MyError Error { get; set; }
    }

    public class PeriodList : List<Period>
    {
        public MyError Error { get; set; }
        public PeriodList()
        {
        }
    }

    public class PeriodDB
    {
        MyDatabase _db = new MyDatabase();

        public Period Save(Period Period)
        {
            try
            {
                SortedList _param = new SortedList();
                _param.Add("@Id", Period.Id);
                _param.Add("@CourseDetailID", Period.CourseDetailId);

                if (Period.Description != string.Empty)
                {
                    _param.Add("@Description", Period.Description);
                }
                else
                {
                    _param.Add("@Description", DBNull.Value);
                }

                _param.Add("@Name", Period.Name);
                _param.Add("@StartDate", Period.StartDate);
                _param.Add("@EndDate", Period.EndDate);

                _param.Add("@IsActive", Period.IsActive);

                int _affRow = _db.ExecuteNonSqlSP("usp_PeriodInsertUpdateSingleItem", _param);

                if (_affRow > 0)
                {
                    Period.Error.ErrorNo = 0;
                }
                else
                {
                    Period.Error.ErrorNo = 1;
                    Period.Error.ErrorMessage = "No row affect when saving school's Period.";
                }
            }
            catch (Exception Ex)
            {
                Period.Error.ErrorNo = 10002;
                Period.Error.ErrorLocation = "Period";
                Period.Error.ErrorMessage = Ex.Message;
                Period.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "Period");
            }

            return Period;
        }

        public PeriodList GetPeriodList(int CompanyId)
        {
            PeriodList retValue = new PeriodList();

            try
            {
                DataTable _dt = new DataTable("tblPeriod");

                SortedList _param = new SortedList();
                _param.Add("@CompanyId", CompanyId);
                _dt = _db.GetDataTableBySP("usp_PeriodSelectItemList", _param);

                if (_dt != null && _dt.Rows.Count > 0)
                {
                    foreach (DataRow _dr in _dt.Rows)
                    {
                        Period _Period = new Period();
                        FillData(_dr, _Period);
                        retValue.Add(_Period);
                    }
                }
            }
            catch (Exception Ex)
            {
                retValue.Error.ErrorNo = 10001;
                retValue.Error.ErrorLocation = "Period";
                retValue.Error.ErrorMessage = Ex.Message;
                retValue.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "Period");
            }

            return retValue;
        }

        public Period GetPeriod(int Id)
        {
            Period retValue = new Period();

            try
            {

                DataTable _dt = new DataTable("tblPeriod");

                SortedList _param = new SortedList();
                _param.Add("@Id", Id);
                _dt = _db.GetDataTableBySP("usp_PeriodSelectSingleItem", _param);

                if (_dt != null && _dt.Rows.Count > 0)
                {
                    FillData(_dt.Rows[0], retValue);
                }
            }
            catch (Exception Ex)
            {
                retValue.Error.ErrorNo = 10001;
                retValue.Error.ErrorLocation = "Period";
                retValue.Error.ErrorMessage = Ex.Message;
                retValue.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "Period");
            }

            return retValue;
        }

        private void FillData(DataRow aRow, Period Period)
        {
            Period.Id = Convert.ToInt32(aRow["Id"].ToString());
            Period.CompanyId = Convert.ToInt32(aRow["CompanyId"].ToString());
            Period.CourseId = Convert.ToInt32(aRow["CourseId"].ToString());
            Period.CourseDetailId = Convert.ToInt32(aRow["CourseDetailId"].ToString());
            
            Period.Error = new MyError();

            if (aRow["Description"] != DBNull.Value)
            {
                Period.Description = aRow["Description"].ToString();
            }
            else
            {
                Period.Description = string.Empty;
            }

            Period.Name = aRow["Name"].ToString();
            Period.CompanyName = aRow["CompanyName"].ToString();
            Period.CourseName = aRow["CourseName"].ToString();
            Period.CourseDetailName = aRow["CourseDetailName"].ToString();

            Period.StartDate = Convert.ToDateTime(aRow["StartDate"].ToString());
            Period.EndDate = Convert.ToDateTime(aRow["EndDate"].ToString());

            Period.CreatedDate = Convert.ToDateTime(aRow["CreatedDate"].ToString());
            Period.ModifiedDate = Convert.ToDateTime(aRow["ModifiedDate"].ToString());

            Period.IsActive = Convert.ToBoolean(aRow["IsActive"].ToString());

            Period.Error.ErrorNo = 0;
        }
    }
}
